package com.study.filter;

import com.study.utils.RedisCache;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import java.io.IOException;
/**
 * JWT 认证过滤器
 */
@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {

    @Autowired
    private RedisCache redisCache;

    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response,
                                    FilterChain filterChain) throws ServletException, IOException {

        String uri = request.getRequestURI();
        // 放行 Swagger/Knife4j 文档相关路径
        if(uri.startsWith("/doc.html") ||
                uri.startsWith("/swagger-ui") ||
                uri.startsWith("/v3/api-docs") ||
                uri.startsWith("/v2/api-docs") ||
                uri.startsWith("/webjars") ||
                uri.startsWith("/swagger-resources")) {
            filterChain.doFilter(request, response);
            return;
        }
        // TODO: 在这里添加 JWT 验证逻辑
        // 例如：从请求头获取 token -> 校验 -> 设置 SecurityContext
        System.out.println("JwtAuthenticationTokenFilter processing URI: " + uri);
        // 放行请求继续执行
        filterChain.doFilter(request, response);
    }
}
